import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:tihai/entity/type.dart';
import 'package:tihai/util/network.dart';
import 'package:tihai/util/storage.dart';

class CommunityPage extends StatefulWidget {
  const CommunityPage({Key? key}) : super(key: key);

  @override
  State<CommunityPage> createState() => _CommunityPageState();
}

class _CommunityPageState extends State<CommunityPage> {
  List<dynamic> listData = [];

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Wrap(
        spacing: 10,
        runSpacing: 20,
        children: getData(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pushNamed(context, "/type");
        },
        child: const Icon(Icons.add),
      ),
    );
  }

  List<Widget> getData() {
    List<ElevatedButton> buttonList = [];
    for (TypeBean value in listData) {
      buttonList.add(ElevatedButton(
        child: Text(value.name),
        onPressed: () async {
          await Navigator.pushNamed(context, "/typeHome", arguments: value);
        },
      ));
    }
    return buttonList;
  }

  void loadData() async {
    var result = await getListAPI(TypeBean(), API.selectTypeList, {});
    setState(() {
      listData = result;
    });
  }
}
